package controller;

import service.UserService;
import service.VehicleService;
import service.impl.UserServiceImpl;
import service.impl.VehicleServiceImpl;
import pojo.User;
import pojo.Vehicle;

import java.util.List;
import java.util.Scanner;

public class CarRentalSystemController {
    private UserService userService = new UserServiceImpl();
    private VehicleService vehicleService = new VehicleServiceImpl();
    private Scanner scanner = new Scanner(System.in);

    public void startSystem() {
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        User user = userService.loginUser(username, password);
        if (user != null) {
            if ("Administrator".equals(user.getUserType())) {
                administratorOperations();
            } else if ("VIPUser".equals(user.getUserType())) {
                userOperations();
            }
        } else {
            System.out.println("登录失败，请重新输入。");
            startSystem();
        }
    }

    private void administratorOperations() {
        boolean loop = true;
        while (loop) {
            System.out.println("管理员操作菜单：");
            System.out.println("1. 查看车辆信息");
            System.out.println("2. 添加车辆");
            System.out.println("3. 修改车辆信息");
            System.out.println("4. 删除车辆");
            System.out.println("5. 查看营业额");
            System.out.println("6. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        viewVehicles();
                        break;
                    case 2:
                        addVehicle();
                        break;
                    case 3:
                        modifyVehicle();
                        break;
                    case 4:
                        deleteVehicle();
                        break;
                    case 5:
                        viewTurnover();
                        break;
                    case 6:
                        loop = false;
                        break;
                    default:
                        System.out.println("无效选择，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入。");
                scanner.nextLine();
            }
        }
    }

    private void userOperations() {
        boolean loop = true;
        while (loop) {
            System.out.println("用户操作菜单：");
            System.out.println("1. 查看可租车车辆");
            System.out.println("2. 租车");
            System.out.println("3. 还车");
            System.out.println("4. 模拟付款");
            System.out.println("5. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        viewAvailableVehicles();
                        break;
                    case 2:
                        rentVehicle();
                        break;
                    case 3:
                        returnVehicle();
                        break;
                    case 4:
                        simulatePayment();
                        break;
                    case 5:
                        loop = false;
                        break